package com.aierkeji.b2c.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aierkeji.b2c.constants.Contants;
import com.aierkeji.b2c.entity.snapshot.AkjAlipayLifeSnap;
import com.aierkeji.b2c.service.AkjAlipayLifeService;
import com.aierkeji.b2c.service.AlipayService;
import com.aierkeji.b2c.utils.AppConfig;
import com.aierkeji.b2c.utils.ResultMapHelper;
import com.aierkeji.b2c.utils.StringUtils;
import com.alipay.servlet.gateway.GatewayServlet;

@RestController
@RequestMapping(value = "gateway")
public class GatewayController {

	private final Logger log = LogManager.getLogger(getClass());
	@Autowired
	AkjAlipayLifeService akjAlipayLifeService;

	@Autowired
	AlipayService alipayService;

	@RequestMapping(value = "/main/{id}", produces = { "application/xml;charset=GBK" })
	public String gateway(@PathVariable("id") String lifeId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		AkjAlipayLifeSnap akjAlipayLifeSnap = akjAlipayLifeService.findById(lifeId);
		log.info("--gateway---",lifeId);
		GatewayServlet gatewayServlet = new GatewayServlet(akjAlipayLifeSnap);
		return gatewayServlet.doPost(request, response);
	}

	@RequestMapping(value = "/main/lifeId/{lifeId}", produces = { "application/xml;charset=GBK" })
	public String gatewayByLifeId(@PathVariable("lifeId") String lifeId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		AkjAlipayLifeSnap akjAlipayLifeSnap = akjAlipayLifeService.findByLifeId(lifeId);
		log.info("--gateway---",lifeId);
		GatewayServlet gatewayServlet = new GatewayServlet(akjAlipayLifeSnap);
		return gatewayServlet.doPost(request, response);
	}
	@RequestMapping(value = "/login/{lifeId}")
	public Map<String, Object> login(@PathVariable("lifeId") String lifeId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.info("in lifeId:{}",lifeId);

		if("dev".equals(AppConfig.getProperty("envType"))) {
			log.info("in dev");
			request.getSession().setAttribute("userId", "2088912537494880");
			request.getSession().setAttribute("lifeId", lifeId);
		}
		if(request.getSession().getAttribute("userId")!=null&&lifeId.equals((String)request.getSession().getAttribute("lifeId"))) {//如果已经有了
			log.info("in had session,userId:{} lifeId:{}",request.getSession().getAttribute("userId"),(String)request.getSession().getAttribute("lifeId"));
			String url = Contants.BASE_URL + "aierkeji-site/b2c-site/#/center";
			response.sendRedirect(url);
			return null;
		}
		request.getSession().removeAttribute("userId");
		log.info("---in firstLogin----");
		String url = akjAlipayLifeService.login(lifeId);
		if (!StringUtils.isEmpty(url)) {
			request.getSession().setAttribute("lifeId", lifeId);
			response.sendRedirect(url);
			return null;
		}
		return ResultMapHelper.nullParamsResult();
	}

	@RequestMapping(value = "/login_back/{lifeId}")
	public Map<String, Object> login_back(@PathVariable("lifeId") String lifeId,
			@RequestParam(name = "auth_code", required = true) String auth_code,
			@RequestParam(name = "app_id", required = true) String app_id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		lifeId = (String) request.getSession().getAttribute("lifeId");
		if (StringUtils.isEmpty(lifeId)) {
			log.info("---no in session lifeId----");
			return ResultMapHelper.nullParamsResult();
		}
		Map<String, Object> map = alipayService.getUserId(lifeId, auth_code);
		if (!CollectionUtils.isEmpty(map)) {
			String userId = (String) map.get("userId");
			request.getSession().setAttribute("userId", userId);
			String url = Contants.BASE_URL + "aierkeji-site/b2c-site/#/center";
			response.sendRedirect(url);
			return null;
		} else {
			return ResultMapHelper.nullParamsResult();
		}
	}

}
